home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / libs / knowhow4 / s_var.h < prev    next >
C/C++ Source or Header  |  1994-10-28  |  4KB  |  92 lines

  1. #ifndef __SLANG_VARIABLE_H_
  2. #define __SLANG_VARIABLE_H_
  3.  
  4. #include "simple.h"
  5. #include "kh_error.h"
  6. #include <alloc.h>
  7. #include <string.h>
  8.  
  9. /*  Variables are ordered in "memory" by addresses. C(HAR), I(NT), L(ONG),
  10.     D(OUBLE) and P(OINTER) are default types, USER_TYPE is user-defined
  11.     type, for example, struct. KNOW-HOW work only in LARGE model, so sizeof(P)
  12.     is 4 bytes. If variable is marked-to-remove,
  13.     adresses[n] -> -adresses[n] - 1 (< 0 in any case). 
  14.     In this case types[n] keep size of this marked area (positive).
  15.     To use pointers arithmetic, Slang should have information
  16.     about type of elements of array. To distinguish between TYPE and pointer
  17.     to this type we use sign of types[n]:
  18.  
  19.     int type, size;
  20.     if(types[n] > 0)
  21.     type = size = types[n];
  22.     else
  23.     { type = P; size = -types[n]; }
  24.     There are two types of arrays: allocated by "new" and pointed in from
  25.     "memory" and arrary in "memory". C++ analog:
  26.     char* s = new char[2];    // In heap
  27.     char s[2];                // In stack
  28.     In Slang in first case types[n] == -C, and memory[arrays[n]] points to
  29.     the array. Size of array element is C, so when we mark it to remove,
  30.     we know how many bytes in "memory" we left. We could call delete to
  31.     delete space occupated by this array.
  32.     In second case types[n] == -C - USER_TYPE, memory[arrays[n]] is not
  33.     pointer but first element of the array. We could not use types[n] to
  34.     determine size of "memory" occupied by array, but we could use
  35.     arrays[n + 1] - arrays[n].
  36.     Array of pointers is the same as array of long (types[n] == -L-USER_TYPE)
  37.     or types[n] == -P).
  38.     If we add array (not pointer) to the memory, we must know the number of
  39.     the elements to copy. If we remove it, we must know the size of free
  40.     memory we get. It is always possible because we have "memory_used"
  41.     for last variable and adresses[n+1] - adresses[n] for others.
  42.  
  43.     Current version does not support user-defined types.
  44. */
  45.  
  46. #define MEMORY_STEP 32//1024
  47. #define ADRESS_STEP 4//64
  48.  
  49. enum { FREE = 0, C = 1, I = 2, L = 4, D = 8, P = 4, USER_TYPE = 16,
  50.        PC = -C-USER_TYPE, PI = -I-USER_TYPE, PL = -L-USER_TYPE,
  51.        PD = -D-USER_TYPE };
  52.  
  53. class Var_Table
  54.     {
  55.     protected:
  56.     long memory_used;
  57.     long memory_size;
  58.     char far* memory;         // Area in memory where Slang keep data
  59.  
  60.     int size;
  61.     int used;
  62.     long* adresses;          // Adress of n-th variable is adresses[n]
  63.     int* types;               // Types of variables FILE
  64.  
  65.     int marked;               // Num. of marked_to_remove
  66.     long mem_marked;         // Size of fragmented memory
  67.  
  68.     int type;                 // Last call to get_type() function set
  69.     int size_of;              // this two variables
  70.  
  71.     int get_type(int n);      // Set type and size variables. Return type.
  72.     int get_size(int n);      // Return size in "memory"
  73.     int check_memory_expand();    // Expand memory if necessary
  74.     int check_memory_compress();  // Remove marked variables if necessary
  75.  
  76.     public:
  77.     Var_Table();
  78.     ~Var_Table();
  79.  
  80.     // Add to position n or to the end variable of type t. Return n (OK) or -1
  81.     int add(void* v, int t, int n = -1, uint elems = 1);
  82.     // Remove variable from the list (and dump if no memory)
  83.     // Return pointer if type is P or NULL. If area is marked-to-remove,
  84.     // types[n] keep size of this area. !!! Pointer to NULL is legal too,
  85.     // but in this case types[n] < 0 !!!
  86.     void* remove(int n, bool check = ON);
  87.     long find(int n);              // Set type and size, return address
  88.     int memory_compress();         // Remove marked variables
  89.     };
  90.  
  91. #endif __SLANG_VARIABLE_H_
  92.